package net.zf.dynamic.threadpool.redis;


import net.zf.dynamic.threadpool.bean.ThreadPoolConfigEntity;
import net.zf.dynamic.threadpool.util.ThreadPoolRegistryEnum;
import org.redisson.api.RBucket;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;

import java.time.Duration;
import java.util.List;

public class RedisRegistry {

    private RedissonClient dynamicRedissonClient;

    public RedisRegistry(RedissonClient dynamicRedissonClient) {
        this.dynamicRedissonClient = dynamicRedissonClient;
    }

    public void reportAllThreadPoolConfig(List<ThreadPoolConfigEntity> threadPoolConfigList) {
        RList<ThreadPoolConfigEntity> threadPoolConfigEntityRList = dynamicRedissonClient.getList(ThreadPoolRegistryEnum.THREAD_POOL_CONFIG_LIST_KEY.getCode());
        threadPoolConfigEntityRList.delete();
        threadPoolConfigEntityRList.addAll(threadPoolConfigList);
    }

    public void reportThreadPoolConfig(ThreadPoolConfigEntity threadPoolConfig) {
       RBucket<ThreadPoolConfigEntity> rThreadPoolConfigBucket = dynamicRedissonClient.getBucket(ThreadPoolRegistryEnum.THREAD_POOL_CONFIG_PARAM+":"+threadPoolConfig.getApplicationName()+":"+threadPoolConfig.getThreadPoolName());
       rThreadPoolConfigBucket.set(threadPoolConfig, Duration.ofDays(30));
    }

}

